﻿using DWH;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using DWH.Utils;
using System.IO;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace RDTest
{
    
    
    /// <summary>
    ///This is a test class for DWHHelperTest and is intended
    ///to contain all DWHHelperTest Unit Tests
    ///</summary>
    [TestClass()]
    public class DWHHelperTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        /// <summary>
        ///A test for GetShortUrlId
        ///</summary>
        [TestMethod()]
        public void GetShortUrlIdTest()
        {
             string[] result = DWHHelper.GetShortUrlId("http://docs.nuget.org/docs/start-here/installing-nuget");
            
        }

        /// <summary>
        ///A test for ExtractPatternFromStringContent
        ///</summary>
        [TestMethod()]
        [DeploymentItem("Data/AICHPicturePageContent.txt","Data")]
        public void ExtractPatternFromStringContentTest()
        {
            string content = File.ReadAllText(@"Data/AICHPicturePageContent.txt");
            string regex = @"<img\b[^<>]*?\bsrc[\s\t\r\n]*=[\s\t\r\n]*[""']?[\s\t\r\n]*(?<imgUrl>[^\s\t\r\n""'<>]*)[^<>]*?/?[\s\t\r\n]*>"; 
            DWHHelper.ExtractPatternFromStringContent(content, regex,"imgUrl");           
        }

        [TestMethod()]
        [DeploymentItem("Data/CLPageContent.txt", "Data")]
        public void ExtractPatternFromStringContentTest2()
        {
            string content = File.ReadAllText(@"Data/CLPageContent.txt");
            List<string> linkUrl = DWHHelper.ExtractPatternFromStringContent(content, DWHConstants.REGEX_HTML_CL_BFXZW, DWHConstants.REGEX_HTML_A_GROUP_LINKURL);
            List<string> urlContents = DWHHelper.ExtractPatternFromStringContent(content, DWHConstants.REGEX_HTML_CL_RMDOWN, DWHConstants.REGEX_HTML_A_GROUP_LINKURL);
        }

        /// <summary>
        ///A test for downloadTorrentJandown
        ///</summary>
        [TestMethod()]
        public void downloadTorrentJandownTest()
        {
            string torrentUrl = "http://www.jandown.com/link.php?ref=pZnJeG3xht";
            string torrentSaveFolder = ".";
            DWHHelper.DownloadTorrentJandown(torrentUrl, torrentSaveFolder);            
        }

        /// <summary>
        ///A test for downloadTorrentUhopefind
        ///</summary>
        [TestMethod()]
        public void downloadTorrentUhopefindTest()
        {
            string torrentUrl = "http://www1.uhopefind.info/link.php?ref=C99P6sWhz6";
            string torrentSaveFolder = "."; 
            DWHHelper.DownloadTorrentUhopefind(torrentUrl, torrentSaveFolder);            
        }

        /// <summary>
        ///A test for DownloadTorrentCLBfxzw
        ///</summary>
        [TestMethod()]
        public void DownloadTorrentCLBfxzwTest()
        {
            string torrentUrl = "http://bfxzw.com/link.php?ref=FkpQW5gPcR"; 
            string torrentSaveFolder = "."; 
            DWHHelper.DownloadTorrentCLBfxzw(torrentUrl, torrentSaveFolder);            
        }

        /// <summary>
        ///A test for DownloadWebPage
        ///</summary>
        [TestMethod()]
        public void DownloadWebPageTest()
        {
            string url = "http://www.rmdown.com/link.php?hash=12211d60cac03a09e984c3e71c9eb5304a587d7f2e2";
            string expected = string.Empty; 
            string actual;
            actual = DWHHelper.DownloadWebPage(url);            
        }

        [DeploymentItem("Data/RmdownPageSample.txt", "Data")]
        [TestMethod()]
        public void DownloadTorrentCLRmdownReffTest()
        {
            string content = File.ReadAllText(@"Data/RmdownPageSample.txt");
            string reffValue = Regex.Match(content, "NAME=\"reff\"\\svalue=\"(?<reff>[A-Za-z0-9]*=*)\"").Groups["reff"].Value;//<INPUT TYPE="hidden" NAME="reff" value="MTM0NTQ4MzA1Mw==">
        }

        /// <summary>
        ///A test for DownloadTorrentCLRmdown
        ///</summary>
        [TestMethod()]
        public void DownloadTorrentCLRmdownTest()
        {
            string torrentUrl = "http://www.rmdown.com/link.php?hash=12211d60cac03a09e984c3e71c9eb5304a587d7f2e2";
            string torrentSaveFolder = "."; 
            DWHHelper.DownloadTorrentCLRmdown(torrentUrl, torrentSaveFolder);            
        }

        /// <summary>
        ///A test for CreateWebAttachmentFolder
        ///</summary>
        [TestMethod()]
        public void CreateWebAttachmentFolderTest()
        {

            DWHHelper.CreateWebAttachmentFolderIfNotExists(@"E:\test");
            DWHHelper.CreateWebAttachmentFolderIfNotExists(@"E:\test\a");
            DWHHelper.CreateWebAttachmentFolderIfNotExists(@"E:\test\a");
            DWHHelper.CreateWebAttachmentFolderIfNotExists(@"E:\test/b");
            DWHHelper.CreateWebAttachmentFolderIfNotExists(@"E:\test/b");
            DWHHelper.CreateWebAttachmentFolderIfNotExists(@"E:\test\c\");
            DWHHelper.CreateWebAttachmentFolderIfNotExists(@"E:\test\c\");
            DWHHelper.CreateWebAttachmentFolderIfNotExists(@"E:\test/d\");
            DWHHelper.CreateWebAttachmentFolderIfNotExists(@"E:\test/d\");           
        }

        /// <summary>
        ///A test for DownloadWebPageImage
        ///</summary>
        [TestMethod()]
        public void DownloadWebPageImageTest()
        {           
            DWHHelper.DownloadWebPageImage("DBPushTask-Image", @"E:/WebPPAttachment/Tumblr/my-sex-addiction\1105063180", "http://25.media.tumblr.com/tumblr_m7ueb2N3ZY1rwmxa1o1_400.gif");           
        }
    }
}
